<template>
  <view class="coupon-container">    <!-- 顶部标签页 -->
    <view class="tab-bar">
      <view 
        v-for="(tab, index) in tabs" 
        :key="index"
        class="tab-item"
        :class="{ active: currentTab === tab.value }"
        @tap="switchTab(tab.value)"
      >
        {{ tab.name }}
      </view>
    </view>

    <!-- 优惠券列表 -->
    <scroll-view class="coupon-list" scroll-y>
      <view v-if="filteredCoupons.length === 0" class="empty-tip">
        暂无优惠券~
      </view>
      <view 
        v-else
        v-for="(coupon, index) in filteredCoupons" 
        :key="index"
        class="coupon-item"
        :class="{ 'coupon-expired': currentTab === 'expired' }"
      >
        <!-- 左侧金额 -->
        <view class="coupon-left" :class="{ 'left-expired': currentTab === 'expired' }">
          <text class="amount">
            <text class="symbol">¥</text>
            {{ coupon.type === 1 ? coupon.amount : (coupon.amount / 10).toFixed(1) }}
          </text>
          <text class="condition">满{{ coupon.minSpend }}可用</text>
        </view>
        
        <!-- 右侧信息 -->
        <view class="coupon-right">
          <view class="coupon-info">
            <text class="title">{{ coupon.title }}</text>
            <text class="desc">{{ coupon.description }}</text>
            <text class="time">{{ formatDate(coupon.startTime) }} - {{ formatDate(coupon.endTime) }}</text>
          </view>
          <view class="coupon-action">
            <button 
              class="use-btn" 
              :class="{ disabled: isExpired(coupon) }"
              @tap="useCoupon(coupon)"
            >
              {{ getButtonText(coupon) }}
            </button>
          </view>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
import { request } from '@/utils/request.js'

export default {
  data() {
    return {
      tabs: [
        { name: '未使用', value: 'unused' },
        { name: '已使用', value: 'used' },
        { name: '已过期', value: 'expired' }
      ],
      currentTab: 'unused',
      coupons: []
    }
  },
  
  computed: {
    filteredCoupons() {
      switch (this.currentTab) {
        case 'unused':
          return this.coupons.filter(coupon => coupon.status === 1)
        case 'used':
          return this.coupons.filter(coupon => coupon.status === 2)
        case 'expired':
          return this.coupons.filter(coupon => coupon.status === 3)
        default:
          return []
      }
    }
  },
  
  onLoad() {
    this.loadCoupons()
  },
  
  methods: {
    async loadCoupons() {
      try {
        const token = uni.getStorageSync('access_token')
        const userInfo = uni.getStorageSync('userInfo')
        
        if (!token || !userInfo) {
          uni.showToast({
            title: '请先登录',
            icon: 'none'
          })
          setTimeout(() => {
            uni.navigateTo({
              url: '/pages/login/login'
            })
          }, 1500)
          return
        }

        const res = await request({
          url: `api/coupons/getCoupons?id=${userInfo.id}`,
          method: 'GET',
          header: {
            'Authorization': token
          }
        })
        
        if (res.code === 200) {
          console.log('获取优惠券成功', res.data);
          this.coupons = res.data
        }
      } catch (error) {
        console.error('获取优惠券失败:', error)
        uni.showToast({
          title: '获取优惠券失败',
          icon: 'none'
        })
      }
    },
    
    switchTab(tab) {
      this.currentTab = tab
    },
    
    formatDate(dateStr) {
      const date = new Date(dateStr)
      return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
    },
    
    isExpired(coupon) {
      return new Date(coupon.endTime) < new Date()
    },
    
    getButtonText(coupon) {
      switch (this.currentTab) {
        case 'unused':
          return '去使用'
        case 'used':
          return '已使用'
        case 'expired':
          return '已过期'
        default:
          return '去使用'
      }
    },
    
    useCoupon(coupon) {
      if (this.currentTab !== 'unused') return
      
      // 这里可以实现使用优惠券的逻辑
      uni.showToast({
        title: '优惠券使用功能开发中',
        icon: 'none'
      })
    }
  }
}
</script>

<style>
.coupon-container {
  min-height: 100vh;
  background: #f5f6fa;
  padding-bottom: env(safe-area-inset-bottom);
}

.tab-bar {
  display: flex;
  background: #fff;
  padding: 20rpx 0;
  position: sticky;
  top: 0;
  z-index: 1;
}

.tab-item {
  flex: 1;
  text-align: center;
  font-size: 28rpx;
  color: #666;
  position: relative;
}

.tab-item.active {
  color: #4080FF;
  font-weight: bold;
}

.tab-item.active::after {
  content: '';
  position: absolute;
  bottom: -10rpx;
  left: 50%;
  transform: translateX(-50%);
  width: 40rpx;
  height: 4rpx;
  background: #4080FF;
  border-radius: 2rpx;
}

.coupon-list {
  padding: 20rpx;
}

.coupon-item {
  display: flex;
  background: #fff;
  border-radius: 12rpx;
  margin-bottom: 20rpx;
  position: relative;
  overflow: hidden;
}

.coupon-item::before {
  content: '';
  position: absolute;
  left: 180rpx;
  top: 0;
  bottom: 0;
  border-left: 2rpx dashed #eee;
}

.coupon-left {
  width: 180rpx;
  padding: 30rpx 20rpx;
  text-align: center;
  background: #4080FF;
  color: #fff;
}

.amount {
  font-size: 48rpx;
  font-weight: bold;
}

.symbol {
  font-size: 32rpx;
}

.condition {
  font-size: 24rpx;
  margin-top: 10rpx;
  display: block;
}

.coupon-right {
  flex: 1;
  padding: 20rpx;
  display: flex;
}

.coupon-info {
  flex: 1;
  padding-right: 20rpx;
}

.title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 10rpx;
  display: block;
}

.desc {
  font-size: 26rpx;
  color: #666;
  margin-bottom: 10rpx;
  display: block;
}

.time {
  font-size: 24rpx;
  color: #999;
  display: block;
}

.coupon-action {
  display: flex;
  align-items: center;
}

.use-btn {
  min-width: 140rpx;
  height: 60rpx;
  line-height: 60rpx;
  text-align: center;
  background: #4080FF;
  color: #fff;
  border-radius: 30rpx;
  font-size: 26rpx;
  margin: 0;
}

.use-btn.disabled {
  background: #ccc;
}

.coupon-expired {
  opacity: 0.6;
}

.coupon-expired .coupon-left {
  background: #999;
}

.left-expired {
  background: #999 !important;
}

.empty-tip {
  text-align: center;
  color: #999;
  font-size: 28rpx;
  padding: 60rpx 0;
}
</style> 